package experiment;

import java.io.IOException;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import metrics.QualityEvaluate;
import metrics.TimeEvaluate;
import summarizer.newversion.ClusterSummarizer;
import summarizer.newversion.CompressedSummarizer;
import summarizer.newversion.DivSummarizer;
import summarizer.newversion.FileSummarizer;
import summarizer.newversion.GreedySummarizer;
import summarizer.newversion.GreedySummarizerItOptNewFilter;
import summarizer.newversion.GreedySummarizerItOptNewFilterSimple;
import summarizer.newversion.GreedySummarizerTweetOptNewFilter;
import summarizer.newversion.GreedySummarizerTweetOptNewFilterSimple;
import summarizer.newversion.RandomSummarizer;
import summarizer.newversion.Summarizer;
import thesis.DataFromFile;
import thesis.DataObject;
import thesis.FSModule;
import thesis.InfoUnit;
import thesis.Summary;

public class MainExp {


	/**
	 * @param args
	 */
	public static void main(String[] args) {

		HashMap<Long, DataObject> memoryTweets = new HashMap<Long, DataObject>();
		List<Dimension> dimensions = new ArrayList<Dimension>();
		ArrayList<Double> weights = new ArrayList<Double>();
		double alpha = 0.3, beta = 0, gamma = 1;
		if (Parameter.fromDB) { // get data from DB
			FSModule.getDataFromDB(memoryTweets, dimensions, weights);
			alpha = FSModule.getQualityWeigth();
			beta = FSModule.getDiversityWeigth();
			gamma = FSModule.getCoverageWeigth();
		} else {
			// get data from File
			try {
				String fileName = "res/toyExample.txt";
				DataFromFile.createToyExample3(fileName);
				DataFromFile.getDataFromFile(fileName, memoryTweets,
						dimensions, weights);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		/***********************************************/
		runSummarizer(memoryTweets, dimensions, weights, alpha, beta, gamma);
	}

	public static void runSummarizer(HashMap<Long, DataObject> memoryTweets,
			List<Dimension> dimensions,
			ArrayList<Double> weights, double alpha, double beta, double gamma) {
		ArrayList<InfoUnit> infoUnits = new ArrayList<InfoUnit>();
		double[][] probs = Preprocess.prepareForSummarization(memoryTweets, dimensions,
				weights, infoUnits);
		if (Parameter.toVisualize && Parameter.fromDB) {
			ArrayList<Summarizer> summarizers = new ArrayList<Summarizer>();
			ArrayList<Summary> summaries = new ArrayList<Summary>();
			summarizers.add(new GreedySummarizerTweetOptNewFilterSimple(probs,
					infoUnits, alpha, 0, 1));
			summarizers.add(new RandomSummarizer());
			//summarizers.add(new ClusterSummarizer());
			for (int i = 0; i < summarizers.size(); i++) {
				summaries.add(summarizers.get(i).computeSummary(10, 1,
						(HashMap<Long, DataObject>) memoryTweets.clone(),
						dimensions.size()));
			}
			visualize(summaries, summarizers, dimensions);
			// FSModule.visualize(summary);
		} else {
			// System.out.printlnf("Correlation: "
			// + Evaluate.corrRougeJS(M, 1, memoryTweets, dimensions.size(),
			// infoUnits, probs));
//			if (Parameter.testTime) {
//				testTime(memoryTweets, dimensions, probs, infoUnits, alpha,
//						beta, gamma);
//			}
			if (Parameter.testQual) {
				testQual(memoryTweets, dimensions, probs, infoUnits, alpha,
						beta, gamma);

			}
		}

	}

	public static void visualize(ArrayList<Summary> summaries,
			ArrayList<Summarizer> summarizers,
			List<Dimension> dimensions) {
		for (int i = 0; i < summaries.size(); i++) {
			Summary summary = summaries.get(i);
			System.out.println("\n =========" + summarizers.get(i)
					+ "============");
			for (DataObject obj : summary.getMemoryTweets()) {
				System.out.println("---------------------");
				System.out.println(obj.getText());
				int count = 0;
				for (Dimension dim : dimensions) {
					System.out.printf("dim %d:", count++);
					for (String key : dim.value2obj.keySet()) {
						Double prob = dim.value2obj.get(key).get(obj.getDbId());
						if (prob != null && prob > 0.1) {
							System.out.printf("(%s %.2f)\t", key, prob);
						}
					}
					System.out.println();
				}
				String timeFormat = "no time";
				Date time = obj.getDate();
				if (time != null) {
					Format format = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
					timeFormat = format.format(time).toString();
				}
				System.out.println("Time: " + timeFormat);
			}
		}

	}

	private static void testTime(final HashMap<Long, DataObject> memoryTweets,
			final List<Dimension> dimensions,
			final double[][] probs, final ArrayList<InfoUnit> infoUnits,
			double alpha, double beta, double gamma) {
		// TESTING VARIABLES
		ArrayList<Summarizer> summarizers = new ArrayList<Summarizer>();
		summarizers.add(new GreedySummarizerItOptNewFilter(probs, infoUnits,
				alpha, 0, 1));
		summarizers.add(new GreedySummarizerTweetOptNewFilter(probs, infoUnits,
				alpha, 0, 1));
		summarizers
				.add(new CompressedSummarizer(probs, infoUnits, alpha, 0, 1));
		// summarizers.add(new ClusterSummarizer());
		// summarizers.add(new RandomSummarizer());
		Summary[] summaries = new Summary[summarizers.size()];
		System.out.println("------Summarizers ---------------");
		for (int i = 0; i < summarizers.size(); i++) {
			System.out.println(i + " : " + summarizers.get(i));
		}
		// test efficiency
		TimeEvaluate.evaluate(summarizers, memoryTweets, dimensions.size());
		// test quality
	}

	private static void testQual(final HashMap<Long, DataObject> memoryTweets,
			final List<Dimension> dimensions,
			final double[][] probs, final ArrayList<InfoUnit> infoUnits,
			double alpha, double beta, double gamma) {
		// TESTING VARIABLES
		ArrayList<Summarizer> summarizers = new ArrayList<Summarizer>();
		// summarizers.add(new GreedySummarizerItOptNewFilterTotal(probs,
		// infoUnits, alpha, beta, gamma));
//		 summarizers.add(new GreedySummarizer(probs, infoUnits, 0.3,
//		 0, 1));

		summarizers.add(new GreedySummarizerItOptNewFilterSimple(probs,
				infoUnits, alpha, 0, 1));
//		summarizers.add(new GreedySummarizerItOptNewFilterSimple(probs,
//				infoUnits, alpha, 0, 1));
//		summarizers.add(new GreedySummarizerItOptNewFilterSimple(probs,
//				infoUnits, 1, 0, 0));
//		summarizers.add(new FileSummarizer());
		summarizers.add(new ClusterSummarizer());
		summarizers.add(new RandomSummarizer());
		// summarizers.add(new PinakiSummarizer(probs, 1, 0, 1));
		System.out.println("------Summarizers ---------------");
		for (int i = 0; i < summarizers.size(); i++) {
			System.out.println(i + " : " + summarizers.get(i));
		}
		// test quality
		QualityEvaluate.evaluate(summarizers, memoryTweets, infoUnits, probs,
				dimensions.size());
	}
}
